package com.yan.annotation;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

@Aspect
@Component
public class ApiValidateAspectss {

    private static final Logger log = LoggerFactory
            .getLogger(ApiValidateAspectss.class);


    @Pointcut("@annotation(com.yan.annotation.ApiValidatess) ")
    private void controllerAspect() {
    }

    @Before("controllerAspect()")
    public void execute(JoinPoint joinPoint)
            throws Throwable {
        log.info("开启验证: brfore 。。。。。。。。。" );
        this.validation(joinPoint);
    }


    @After("controllerAspect()")
    public void after() {

        log.info("开启验证: after。。。。。。。。。????/" );

    }


    public void validation(JoinPoint joinPoint)
            throws Throwable {

        String methodName = joinPoint.getSignature().getName();
        Method method = currentMethod(joinPoint, methodName);
        // ApiValidate apiValidate = method.getAnnotation(ApiValidate.class);

        log.info("开启验证:" );


    }

    public Method currentMethod(JoinPoint joinPoint, String methodName) {
        Method[] methods = joinPoint.getTarget().getClass().getMethods();
        Method resultMethod = null;
        for (Method method : methods) {
            System.out.println("ApiValidateAspect: " + method.getName());
            if (method.getName().equals(methodName)) {
                resultMethod = method;
                break;
            }
        }
        return resultMethod;
    }
}
